/*


	jQuery plugin: formExtendLabel
	Author: Ian Hart at Grand Union [ianh@thegrandunion.com]


	What?
	=====
	A super, simple plugin for extending a label's click event to another element.


	How?
	====
	Easy. Specify the selector for the label:
	
		$('label.extend-me').extendLabel();
		
	By default, the ancestor it extends to is a <p> element. However, it is recommended 
	that you specify an ancestor selector via the options since this will avoid assigning 
	unwanted click events to elements further up the DOM tree. For example:
	
		HTML:
	
			<ul class="my-list">
				<li>
					<img src="../img/my-image.png"/>
					<label for="my-radio">My Radio Button</label>
					<input type="radio" id="my-radio" />
				</li>
				<li>
					<img src="../img/my-image-2.png"/>
					<label for="my-radio-2">My Radio Button 2</label>
					<input type="radio" id="my-radio-2" />
				</li>
			</ul>
		
		JavaScript:
	
			$('.my-list label').formExtendLabel({
				ancestorSelector : '.my-list li'
			});
		
	This will mean, clicking any part of an <li> element (including the <img> tag) will 
	make its radio button checked.


	Options
	=======
	ancestorSelector : A string representing the ancestor Selector to which the label's click event extends.
		
	
	Dependencies
	============
	- jQuery 1.3.2 or higher (jquery.com)
	


*/
(function($){

	$.fn.formExtendLabel = function(settings){

		var G = {
			config : {
				ancestorSelector : 'p'
			},
			$this : this
		};
		
		if(settings){ $.extend(G.config, settings); }


		function init(){

			G.$this.parents(G.config.ancestorSelector).click(function(){
				$('#'+$('label', this).attr('for')).attr('checked','checked');
				$('.selected', $(this).parent()).removeClass('selected'); // Remove 'selected' class from any siblings.
				$(this).addClass('selected');
			})
			.css('cursor','pointer');

		}
		
		
		init();
		return this;
	
	};

})(jQuery);